﻿using MathNet.Numerics;
using MathNet.Numerics.Interpolation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Model;
using UtilZ.Dotnet.MathNetEx.Interpolation;
using UtilZ.Dotnet.MathNetEx.LinearAlgebra;

namespace ConAppCore.Test
{
    internal class TestMathNetEx
    {
        public static void Test()
        {
            //TestLinearSplineEx();
            //TestFitEx();
            TestPolynomial();
        }

        private static void TestPolynomial()
        {
            string filePath = @"D:\sigALL\QPSK\2_5M\20240618141817_IQ_AAAA041108ZD01_F_0500000000_B_0005000000_S_0006400000_ns_2744851981.wav";
            int count = 100;
            double[] x = new double[count];
            double[] y = new double[count];

            using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            var reader = new BinaryReader(fileStream);
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = i + 1;
                y[i] = reader.ReadInt16();
            }
            double[] Coiefs = Fit.Polynomial(x, y, 2);  // 二次拟合



            var xPointer = new UnmanagedMemoryPointer<double>(count);
            var yPointer = new UnmanagedMemoryPointer<double>(count);
            var coiefsPointer = new UnmanagedMemoryPointer<double>(count);
            SpanZ<double> xs = xPointer.GetSpanZ();
            SpanZ<double> ys = yPointer.GetSpanZ();
            SpanZ<double> coiefs = coiefsPointer.GetSpanZ();

            fileStream.Position = 0;
            for (int i = 0; i < ys.Length; i++)
            {
                xs[i] = i + 1;
                ys[i] = reader.ReadInt16();
            }
            //FitEx.Polynomial(xs, ys, 2, ref coiefs);  // 二次拟合
        }


        private static void TestFitEx()
        {
            double[] x = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            double[] y = new double[] { 6, 7, 8, 9, 10, 11, 12, 13, 14 };
            Polynomial polyFit = Polynomial.Fit(x, y, 2, MathNet.Numerics.LinearRegression.DirectRegressionMethod.QR);
            double[] coefficients = polyFit.Coefficients;

            Polynomial polyFit2 = PolynomialEx.Fit(x, y, 2, MathNet.Numerics.LinearRegression.DirectRegressionMethod.QR);
            double[] coefficients2 = polyFit.Coefficients;
        }

        private static void TestLinearSplineEx()
        {
            int len = 2560;
            using var xPointer = new UnmanagedMemoryPointer<double>(len);
            using var yPointer = new UnmanagedMemoryPointer<double>(len);
            SpanZ<double> x = xPointer.GetSpanZ();
            SpanZ<double> y = yPointer.GetSpanZ();
            Random rnd = new Random();
            for (int i = 0; i < len; i++)
            {
                x[i] = rnd.Next(-300, 300);
                y[i] = rnd.Next(-300, 300);
            }

            LinearSpline linearSpline = LinearSpline.InterpolateSorted(x.ToArray(), y.ToArray());
            LinearSplineEx linearSplineEx = LinearSplineEx.InterpolateSorted(x, y);

            double[] ret1 = new double[len];
            double[] ret2 = new double[len];


            for (int i = 0; i < len; i++)
            {
                ret1[i] = linearSpline.Interpolate(i);
                ret2[i] = linearSplineEx.Interpolate(i);
            }

        }

    }

}
